const options = @options();
const welcomeSettings = {};

const slimeStoreEventKey = "qrscene_slime.store@";

@lab(function (lab) {
    return  @.async.all(@.keys(options),-1, async function (optionName) {
        if (optionName.indexOf("weixinmp") >= 0 && options[optionName].welcome){
            await @promise(`${optionName}.ready`);
            try {
                lab.slime_subscribe.weixinmp.@configWelcome(optionName, options[optionName].welcome);
            } catch (ex) {
                @error(ex);
            }
        }
    })
}).rejected(@error);

const onReceiveMessage = function (weixinmp) {
    return function (mew) {
        
        var settings = welcomeSettings[weixinmp];
        var { FromUserName , EventKey } = mew.content[mew.usage];
        
        @lab(function (lab) {

            return @.async(function () {

                if (EventKey && EventKey.indexOf(slimeStoreEventKey) > -1) {

                    lab.find("weixinmp_qrcode_scenes",EventKey.substr("qrscene_".length)).then(function (scene) {

                        if(scene && scene.scene_id) {
                            lab.find("slime_stores",scene.scene_id).pipe(this);
                        } else {
                            this.next();
                        }

                    }).pipe(this);

                } else {
                    this.next();
                }

            }).then(function (store) {

                let title = @.format(settings.text, {
                    store
                })

                switch (settings.type) {
                    case "miniprogram" : {
                        return  lab[weixinmp.split(".").join("_")].api.@getMediaIDByURL(settings.image).then(function (media_id) {
                            lab[weixinmp.split(".").join("_")].kefu.@sendMiniProgramCard({ 
                                open_id : FromUserName    , 
                                title   : title  , 
                                app_id  : settings.appID  ,
                                page_path : settings.page ,
                                thumb_media_id : media_id
                            }).pipe(this);
                        })
                    }
                    case "text" : {

                        if (title) {

                            return lab[weixinmp.split(".").join("_")].kefu.@sendTextMsg({ 
                                open_id : FromUserName    , 
                                content   : title  , 
                            })

                        } else {
                            return @.async.resolve();
                        }
                        
                    }

                    default : {
                        return @.async.resolve();
                    }
                }

            })

            

        }).rejected(@error)
        
        
    }    
}

module.exports = function (lab) {
    return {
        @configWelcome : function (weixinmp, settings) {

            
            let mergedSettings = @.merge.advanced({
                "type" : {
                    "!valueType" : "string",
                    "!defaultValue" : "text"
                },
                "appID" : {
                    "!valueType" : "string",
                },
                "text"  : {
                    "!valueType" : "string",
                },
                "page"  : {
                    "!valueType" : "string",
                }
            },settings || {});

            // TODO 这里要做参数校验

            if (!welcomeSettings[weixinmp]) {
                @heard([`${weixinmp}.subscribe.event`]).then(onReceiveMessage(weixinmp));
            }

            welcomeSettings[weixinmp] = mergedSettings;

        }
    }
}